package com.bingxue.edu.common.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.bingxue.edu.common.config.WebsiteProperties;
import com.bingxue.edu.framework.common.model.LabelValue;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.enums.DictEnums;
import com.bingxue.edu.management.system.model.entity.ErrorLog;
import com.bingxue.edu.management.system.model.entity.Org;
import com.bingxue.edu.management.system.service.DictItemService;
import com.bingxue.edu.management.system.service.ErrorLogService;
import com.bingxue.edu.management.system.service.OrgService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公共 API
 *
 * @author panbo
 * @since 2024/6/28
 */
@SuppressWarnings({"rawtypes"})
@Tag(name = "公共接口")
@SaIgnore
@RequestMapping
@RestController
@RequiredArgsConstructor
public class CommonController {

    private final WebsiteProperties websiteProperties;
    private final DictItemService dictItemService;
    private final OrgService orgService;
    private final ErrorLogService errorLogService;

    @GetMapping("/")
    @Operation(summary = "首页")
    public String index() {
        return "service running!";
    }

    @SaIgnore
    @Operation(summary = "查询所有字典")
    @GetMapping("/common/dict")
    public Response<Map<String, List<LabelValue>>> listDict() {
        Map<String, List<LabelValue>> result = new HashMap<>(DictEnums.getEnumMap());
        // 自定义字典
        result.putAll(dictItemService.listDictMap());
        return Response.success(result);
    }

    @Operation(summary = "查询网站配置")
    @GetMapping("/common/config/website")
    public Response<WebsiteProperties> getWebsiteConfig() {
        return Response.success(websiteProperties);
    }

    @Operation(summary = "查询校区")
    @GetMapping("/common/campus/list")
    public Response<List<Org>> listCampus() {
        return Response.success(orgService.getCampusList());
    }

    @Operation(summary = "提交错误")
    @PostMapping("/common/error/submit")
    public Response<Void> submitError(@RequestBody ErrorLog errorLog) {
        errorLogService.create(errorLog);
        return Response.success();
    }
}
